package com.qf.air.interceptor;

import com.qf.air.entity.User;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author: 维多利加
 * @Date: 2021/3/25 19:20
 */
@Component
public class AuthenticationInterceptor implements HandlerInterceptor {


    /**
     * 前处理
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //1. 从session中查询用户信息
        User user = (User) request.getSession().getAttribute("user");

        //2.如果登录了，return true，如果没登陆 return false
        if(user != null){
            return true;
        }else{
            response.sendRedirect(request.getContextPath()+"/login.html");
            return false;
        }
    }

}
